<?php

namespace app\common\command;

use addons\shopro\service\Wallet as WalletService;
use app\common\model\shopro\UserTeamDayWalletLogModel;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Gamewater extends Command
{
    protected function configure()
    {
        $this->setName('gamewater')->setHelp('游戏流水返佣')->setDescription('游戏流水返佣');
    }

    protected function execute(Input $input, Output $output)
    {
        exit();
            Db::startTrans();
            try {
                $dayLogModel = new UserTeamDayWalletLogModel();
//                $data        = $dayLogModel->where('status','=',0)->whereTime('endtime','d')->select();
                $data        = $dayLogModel->where('status','=',0)->select();
                if($data){
                    foreach ($data as $k=>$v){
                        $userInfo = User::where('level','>',0)->where('id','=',$v['user_id'])->with(['acceleration'])->field('id,level')->find();
                        if($userInfo && $userInfo->acceleration){
                            $amount = bcmul($v['amount'], $userInfo->acceleration->dividend,2)/100;
                            if($amount > 0){
                                WalletService::change($v['user_id'], 'score', $amount, 'team_gamewater',[],
                                    '团队游戏流水返利');
                            }
                        }
                        $v->status = 1;
                        $v->save();
                    }
                }
                Db::commit();
            } catch (HttpResponseException $e) {
                Db::rollback();
                $data    = $e->getResponse()->getData();
                $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                format_log_error($e, 'Rebatemoney.HttpResponseException', $message);
            }
    }

}
